package com.example.demo;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;

import java.util.Set;

public class WvJsLoadURLActivity extends AppCompatActivity {
  private WebView webView_jsLoadURl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_js_load_url);
        webView_jsLoadURl = findViewById(R.id.webView_jsLoadURl);

        webView_jsLoadURl.getSettings().setJavaScriptEnabled(true);
        webView_jsLoadURl.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        webView_jsLoadURl.loadUrl("file:///android_asset/test2.html");
        webView_jsLoadURl.getSettings().setDefaultTextEncodingName("UTF-8");

        webView_jsLoadURl.setWebChromeClient(new WebChromeClient(){
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")){
                    if (uri.getAuthority().equals("jumpToActivityB")){
                        String id = uri.getQueryParameter("id");
                        Intent intent = new Intent(WvJsLoadURLActivity.this,ActivityB.class);
                        intent.putExtra("id",id);
                        startActivity(intent);
                    }
                }
                   result.confirm();
                   return true;
            }

            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                return super.onJsConfirm(view, url, message, result);
            }

            @Override
            public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,final JsPromptResult result) {
                Uri uri = Uri.parse(message);
                if (uri.getScheme().equals("js")){
                    if ( uri.getAuthority().equals("getName")){
                        final EditText editText = new EditText(WvJsLoadURLActivity.this);
                        AlertDialog alertDialog = new AlertDialog.Builder(WvJsLoadURLActivity.this)
                                .setTitle("请输入姓名")
                                .setView(editText)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                       String name = editText.getText().toString();
                                       result.confirm(name);
                                       dialog.cancel();
                                    }
                                }).create();
                        alertDialog.show();
                    }
                }
                return true;
            }

        });

        webView_jsLoadURl.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                Uri uri = Uri.parse(url);
                if(uri.getScheme().equals("js")){
                    if (uri.getAuthority().equals("webview")){
                        Set<String> paramSet = uri.getQueryParameterNames();
                        for(String param : paramSet){
                            String value = uri.getQueryParameter(param);
                            Log.e("WvJsLoadURLActivity","  param="+param+"  value="+value);
                        }
                    }
                }


                view.loadUrl(url);
                return true;
            }
        });

    }
}
